package com.example.teacher.Controller;


import com.example.common.constants.OtherConstants;
import com.example.common.domain.Result;
import com.example.common.domain.dto.SaveSection;
import com.example.common.domain.dto.SectionDto;
import com.example.common.domain.dto.WorkDto;
import com.example.common.domain.pojo.*;
import com.example.common.utils.UUIDUtil;
import com.example.teacher.Converter.QuestionConverter;
import com.example.teacher.Service.*;
import com.example.teacher.domain.dto.QuestionSaveDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(value = "老师端接口", description = "保存ai生成数据")
@RestController
@RefreshScope//时时刷新配置信息
@Slf4j  //日志  log.info("登录日志测试：");
@RequestMapping("/teachers")
@RequiredArgsConstructor
public class AiSaveController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private HomeworkService homeworkService;
    @Autowired
    private CourseOutlineService outlineService;
    @Autowired
    private SectionService sectionService;
    @Autowired
    private PPTService pptService;
    @Autowired
    private HandoutsService handoutsService;
    @Autowired
    private ExperimentService experimentService;
    @Autowired
    private RethinkService rethinkService;

    @ApiOperation("保存大纲")
    @PostMapping("/saveOutline")
    public Result<CourseOutline> saveOutline(@RequestBody CourseOutline outline) {
        outlineService.saveOutline(outline);
        return Result.success(outline);
    }

    @ApiOperation("保存小节")
    @PostMapping("/saveSection")
    public Result<Section> saveSection(@RequestBody SaveSection save) {
        SectionDto sectionDto = save.getSectionDto();
        String sectionId = sectionDto.getCourseId() + sectionDto.getChapterId() + sectionDto.getSectionId();
        Section section = save.getSection();
        section.setSectionId(sectionId);
        sectionService.saveSection(section);
        return Result.success(section);
    }

    @ApiOperation("保存作业,在websocket的时候用的")
    @PostMapping("/WsSaveHomework")
    public Result<Void> WsSaveHomework(@RequestBody WorkDto workDto) {
        homeworkService.saveHomework(workDto);
        return Result.success();
    }

    @ApiOperation("保存作业,如果是http请求用这个")
    @PostMapping("/saveHomework")
    public Result<Homework> saveHomework(@RequestBody Homework homework) {
        System.out.println(homework.getStartTime());
        System.out.println(homework.getUpdateTime());
        homeworkService.InsertHomework(homework);
        return Result.success(homework);
    }

    @ApiOperation("保存问题,单个问题保存")
    @PostMapping("/saveQuestion")
    public Result<Void> saveQuestion(@RequestBody Question question) {
        questionService.saveQuestion(question);
        return Result.success();
    }

    @ApiOperation("批量保存问题")
    @PostMapping("/saveQuestions")
    public Result<Void> saveQuestions(@RequestBody List<Question> questions) {
        questionService.MoreSaveQuestion(questions);
        return Result.success();
    }

    @ApiOperation("保存问题,单个问题保存dto")
    @PostMapping("/saveQuestion2")
    public Result<Void> saveQuestion2(@RequestBody QuestionSaveDto dto) {
        Question question = QuestionConverter.convertToQuestion(dto);
        question.setId(UUIDUtil.generate(OtherConstants.ID_LENGTH));
        questionService.saveQuestion(question);
        return Result.success();
    }

    @ApiOperation("批量保存问题dto")
    @PostMapping("/saveQuestions2")
    public Result<Void> saveQuestions2(@RequestBody List<QuestionSaveDto> dtos) {
        List<Question> questions = QuestionConverter.convertToQuestionList(dtos);
        questionService.MoreSaveQuestion(questions);
        return Result.success();
    }

    @ApiOperation("保存PPT")
    @PostMapping("/savePPT")
    public Result<Ppt> savePPT(@RequestBody Ppt ppt) {
        ppt.setId(UUIDUtil.generate(OtherConstants.ID_LENGTH));
        pptService.savePPT(ppt);
        return Result.success(ppt);
    }

    @ApiOperation("保存讲义")
    @PostMapping("/saveHandouts")
    public Result<Handouts> saveHandouts(@RequestBody Handouts handouts) {
        handouts.setId(UUIDUtil.generate(OtherConstants.ID_LENGTH));
        handoutsService.saveHandouts(handouts);
        return Result.success(handouts);
    }

    @ApiOperation("保存实验")
    @PostMapping("/saveExperiment")
    public Result<Experiment> saveExperiment(@RequestBody Experiment experiment) {
        experiment.setId(UUIDUtil.generate(OtherConstants.ID_LENGTH));
        experimentService.saveExperiment(experiment);
        return Result.success(experiment);
    }

    @ApiOperation("保存老师反思")
    @PostMapping("/saveRethink")
    public Result<Void> saveRethink(@RequestBody Rethink rethink) {
        rethink.setId(UUIDUtil.generate(OtherConstants.ID_LENGTH));
        rethinkService.save(rethink);
        return Result.success();
    }


}
